home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / run-mailcap < prev    next >
Encoding:
Text File  |  2009-12-08  |  16.5 KB  |  565 lines

  1. #! /usr/bin/perl
  2. ###############################################################################
  3. #
  4. #  Run-Mailcap:  Run a program specified in the mailcap file based on a mime
  5. #  type.
  6. #
  7. #  Written by Brian White <bcwhite@pobox.com>
  8. #  This file has been placed in the public domain (the only true "free").
  9. #
  10. ###############################################################################
  11.  
  12.  
  13. $debug=0;
  14. $norun=0;
  15. $nopager=0;
  16. $etcmimetyp="/etc/mime.types";
  17. $shrmimetyp="/usr/share/etc/mime.types";
  18. $locmimetyp="/usr/local/etc/mime.types";
  19. $usrmimetyp="$ENV{HOME}/.mime.types";
  20. $xtermprgrm="/usr/bin/x-terminal-emulator"; # xterm?
  21. $defmimetyp="application/octet-stream";
  22. $quotedsemi=chr(255);
  23. $quotedprct=chr(254);
  24. $retcode=0;
  25.  
  26.  
  27. %patterntypes =
  28. (
  29.  '(^|/)crontab[^/]+$'                           => 'text/x-crontab',            #'
  30.  '/man\d*/'                                     => 'application/x-troff-man',   #'
  31.  '\.\d[^\.]*$'                                  => 'application/x-troff-man',   #'
  32. );
  33.  
  34.  
  35.  
  36. sub Usage {
  37.     my($error) = @_;
  38.     print STDERR $error,"\n\n" if $error;
  39.  
  40.     print STDERR "Use: $0 <--action=VAL> [--debug] [MIME-TYPE:[ENCODING:]]FILE [...]\n\n";
  41.     print STDERR "Options:\n";
  42.     print STDERR "  action        specify what action to do on these files (default=view)\n";
  43.     print STDERR "  debug         be verbose about what's going on\n";
  44.     print STDERR "  nopager       ignore any \"copiousoutput\" directives and never use a \"pager\"\n";
  45.     print STDERR "  norun         just print but don't execute the command (useful with --debug)\n";
  46.     print STDERR "\n";
  47.     print STDERR "Mime-Type:\n";
  48.     print STDERR "  any standard mime type designation in the form <class>/<subtype> -- if\n";
  49.     print STDERR "  not specified, it will be determined from the filename extension\n\n";
  50.     print STDERR "Encoding:\n";
  51.     print STDERR "  how the file (and type) has been encoded (only \"gzip\", \"bzip\", \"bzip2\"\n";
  52.     print STDERR "  and \"compress\" are supported) -- if not specified, it will be determined\n";
  53.     print STDERR "  from the filename extension\n\n";
  54.  
  55.     exit ($error ? 1 : 0);
  56. }
  57.  
  58.  
  59.  
  60. sub EncodingForFile {
  61.     my($file) = @_;
  62.     my $encoding;
  63.  
  64.     if ($file =~ m/\.gz$/)  { $encoding = "gzip";       }
  65.     if ($file =~ m/\.bz$/)  { $encoding = "bzip";       }
  66.     if ($file =~ m/\.bz2$/) { $encoding = "bzip2";      }
  67.     if ($file =~ m/\.Z$/)   { $encoding = "compress";   }
  68.  
  69.     print STDERR " - file \"$file\" has encoding \"$encoding\"\n" if $debug && $encoding;
  70.  
  71.     return $encoding;
  72. }
  73.  
  74.  
  75.  
  76. sub ReadMimetypes {
  77.     my($file) = @_;
  78.  
  79.     return unless -r $file;
  80.  
  81.     print STDERR " - Reading mime.types file \"$file\"...\n" if $debug;
  82.     open(MIMETYPES,"<$file") || die "Error: could not read \"$file\" -- $!\n";
  83.     while (<MIMETYPES>) {
  84.         chomp;
  85.         s/\#.*$//;
  86.         next if (m/^\s*$/);
  87.  
  88.         $_=lc($_);
  89.         my($type,@exts) = split;
  90.  
  91.         foreach (@exts) {
  92.             $mimetypes{$_} = $type unless exists $mimetypes{$_};
  93.         }
  94.     }
  95.     close MIMETYPES;
  96. }
  97.  
  98.  
  99.  
  100. sub ReadMailcap {
  101.     my($file) = @_;
  102.     my $line = "";
  103.  
  104.     return unless -r $file;
  105.  
  106.     print STDERR " - Reading mailcap file \"$file\"...\n" if $debug;
  107.     open(MAILCAP,"<$file") || die "Error: could not read \"$file\" -- $!\n";
  108.     while (<MAILCAP>) {
  109.         chomp;
  110.         s/^\s+// if $line;
  111.         $line .= $_;
  112.         next unless $line;
  113.         if ($line =~ m/^\s*\#/) {
  114.             $line = "";
  115.             next;
  116.         }
  117.         if ($line =~ m/\\$/) {
  118.             $line =~ s/\\$//;
  119.         } else {
  120.             $line =~ s/\\;/$quotedsemi/go;
  121.             $line =~ s/\\%/$quotedprct/go;
  122.             push @mailcap,$line;
  123.             $line = "";
  124.         }
  125.     }
  126.     close MAILCAP;
  127. }
  128.  
  129.  
  130.  
  131. sub TempFile {
  132.     my($template) = @_;
  133.     my($cmd,$head,$tail,$tmpfile);
  134.     $template = "" unless (defined $template);
  135.  
  136.     ($head,$tail) = split(/%s/,$template,2);
  137.  
  138. #   $tmpfile = POSIX::tmpnam($name);
  139. #   unlink($tmpfile);
  140.  
  141.     $cmd  = "tempfile --mode=600";
  142.     $cmd .= " --prefix $head" if $head;
  143.     $cmd .= " --suffix $tail" if $tail;
  144.  
  145.     $tmpfile = `$cmd`;
  146.     chomp($tmpfile);
  147.  
  148. #   $tmpfile = $ENV{TMPDIR};
  149. #   $tmpfile = "/tmp" unless $tmpfile;
  150. #   $tmpfile.= "/$name";
  151. #   unlink($tmpfile);
  152.  
  153.     return $tmpfile;
  154. }
  155.  
  156.  
  157.  
  158. sub SaveStdin {
  159.     my($match) = @_;
  160.     my($tmpfile,$amt,$buf);
  161.  
  162.     $tmpfile = $1 if ($match =~ m/nametemplate=(.*?)\s*($|;)/);
  163.     $tmpfile = TempFile($tmpfile);
  164.     open(TMPFILE,">$tmpfile") || die "Error: could not write \"$tmpfile\" -- $!\n";
  165.     do {
  166.         $amt = read(STDIN,$buf,102400);
  167.         print TMPFILE $buf if $amt;
  168.     } while ($amt != 0);
  169.     close(TMPFILE);
  170.  
  171.     return $tmpfile;
  172. }
  173.  
  174.  
  175.  
  176. sub DecodeFile {
  177.     my($efile,$encoding,$action) = @_;
  178.     my($file,$res);
  179.  
  180.     $file = $efile;
  181.     $file =~ s!^.*/!!;          # remove leading directories
  182.     $file =~ s!\.[^\.]*$!!;     # remove encoding extension
  183.     $file =~ s!^\.?[^\.]*!%s!;  # replace name with placeholder
  184.     $file = undef if ($efile eq '-');
  185.     my $tmpfile = TempFile($file);
  186.  
  187.     print STDERR " - decoding \"$efile\" as \"$tmpfile\"\n" if $debug;
  188.  
  189. #   unlink($tmpfile); # should still be acceptable for "compose" output even if exists
  190.     return $tmpfile if (($efile ne '-' && ! -e $efile) || $action eq 'compose');
  191.  
  192.     if ($encoding eq "gzip") {
  193.         if ($efile eq '-') {
  194.             $res = system "gzip -d >\Q$tmpfile\E";
  195.         } else {
  196.             $res = system "gzip -dc \Q$efile\E >\Q$tmpfile\E";
  197.         }
  198.     } elsif ($encoding eq "bzip") {
  199.         if ($efile eq '-') {
  200.             $res = system "bzip -d >\Q$tmpfile\E";
  201.         } else {
  202.             $res = system "bzip -dc <\Q$efile\E >\Q$tmpfile\E";
  203.         }
  204.     } elsif ($encoding eq "bzip2") {
  205.         if ($efile eq '-') {
  206.             $res = system "bzip2 -d >\Q$tmpfile\E";
  207.         } else {
  208.             $res = system "bzip2 -dc <\Q$efile\E >\Q$tmpfile\E";
  209.         }
  210.     } elsif ($encoding eq "compress") {
  211.         if ($efile eq '-') {
  212.             $res = system "uncompress >\Q$tmpfile\E";
  213.         } else {
  214.             $res = system "uncompress <\Q$efile\E >\Q$tmpfile\E";
  215.         }
  216.     } else {
  217.         die "Fatal: unknown encoding \"$encoding\" at";
  218.     }
  219.  
  220.     $res = int($res/256);
  221.     if ($res != 0) {
  222.         print STDERR "Error: could not decode \"$efile\" -- $!\n";
  223.         $retcode = 2 if ($retcode < 2);
  224.         unlink($tmpfile);
  225.         return;
  226.     }
  227.  
  228. #   chmod 0600,$tmpfile; # done already by TempFile
  229.     return $tmpfile;
  230. }
  231.  
  232.  
  233.  
  234. sub EncodeFile {
  235.     my($dfile,$efile,$encoding) = @_;
  236.     my($res);
  237.  
  238.     print STDERR " - encoding \"$dfile\" as \"$efile\"\n";
  239.  
  240.     if ($encoding eq "gzip") {
  241.         if ($efile eq '-') {
  242.             $res = system "gzip -c \Q$dfile\E";
  243.         } else {
  244.             $res = system "gzip -c \Q$dfile\E >\Q$efile\E";
  245.         }
  246.     } elsif ($encoding eq "compress") {
  247.         if ($efile eq '-') {
  248.             $res = system "compress <\Q$dfile\E";
  249.         } else {
  250.             $res = system "compress <\Q$dfile\E >\Q$efile\E";
  251.         }
  252.     } else {
  253.         die "Fatal: unknown encoding \"$encoding\" at";
  254.     }
  255.  
  256.     $res = int($res/256);
  257.     if ($res != 0) {
  258.         print STDERR "Error: could not encode \"$efile\" (left as \"$dfile\")\n";
  259.         $retcode = 2 if ($retcode < 2);
  260.         return;
  261.     }
  262.  
  263.     return $dfile;
  264. }
  265.  
  266.  
  267.  
  268. sub ExtensionMimetype {
  269.     my($ext) = @_;
  270.     my($typ);
  271.  
  272.     unless ($donemimetypes) {
  273.         ReadMimetypes($usrmimetyp);
  274.         ReadMimetypes($locmimetyp);
  275.         ReadMimetypes($shrmimetyp);
  276.         ReadMimetypes($etcmimetyp);
  277.         $donemimetypes = 1;
  278.     }
  279.  
  280.     $typ = $mimetypes{lc($ext)};
  281.  
  282.     print STDERR " - extension \"$ext\" maps to mime-type \"$typ\"\n" if $debug;
  283.     return $typ;
  284. }
  285.  
  286.  
  287.  
  288. sub PatternMimetype {
  289.     my($file) = @_;
  290.     my($key,$val);
  291.  
  292.     while (($key,$val) = each %patterntypes) {
  293.         if ($file =~ m!$key!i) {
  294.             print STDERR " - file \"$file\" maps to mime-type \"$val\"\n" if $debug;
  295.             return $val;
  296.         }
  297.     }
  298.  
  299.     print STDERR " - file \"$file\" does not conform to any known pattern\n" if $debug;
  300.     return;
  301. }
  302.  
  303.  
  304.  
  305. sub FileMimetype {
  306.     my($file) = @_;
  307.     my($ext)  = ($file =~ m!\.([^/\.]+)$!);
  308.  
  309.     my $type;
  310.  
  311.     $type = ExtensionMimetype($ext) if $ext;
  312.     $type = PatternMimetype($file) unless $type;
  313.  
  314.     return $type;
  315. }
  316.  
  317.  
  318.  
  319. @files = ();
  320. foreach (@ARGV) {
  321.     print STDERR " - parsing parameter \"$_\"\n" if $debug;
  322.     if (m!^(-h|--help)$!) {
  323.         Usage();
  324.         exit(0);
  325.     } elsif (m!^--(.*?)=(.*)$!) {
  326.         print STDERR "Warning: definition of \"$1=$2\" overrides value \"${$1}\"\n" if ($ {$1} && $ {$1} != $2);
  327.         $ {$1}=$2;
  328.     } elsif (m!^--(.*?)$!) {
  329.         print STDERR "Warning: definition of \"$1=$2\" overrides value \"${$1}\"\n" if ($ {$1} && $ {$1} != 1);
  330.         $ {$1}=1;
  331.     } elsif (m!^[^/:]+/[^/:]+:[^/:]+:!) {
  332.         push @files,$_;
  333.     } elsif (m!^([^/:]+/[^/:]+):(.*)! && ! -e $_) {
  334.         my $file = $_;
  335.         my $type = $1;
  336.         my $file = $2;
  337.         my $code = EncodingForFile($file);
  338.         push @files,"${type}:${code}:${file}";
  339.         print STDERR " - file \"$file\" does not exist -- assuming mime-type specification of \"${type}\"\n" if $debug;
  340.     } else {
  341.         my $file = $_;
  342.         my $code = EncodingForFile($file);
  343.         my $type;
  344.         if ($code) {
  345.             my $efile = $file;
  346.             $efile =~ s/\.[^\.]+$//;
  347.             $type = FileMimetype($efile);
  348.         } else {
  349.             $type = FileMimetype($file);
  350.         }
  351.         if ($type) {
  352.             push @files,"${type}:${code}:${file}";
  353.         } else {
  354.             print STDERR "Warning: unknown mime-type for \"$file\" -- using \"$defmimetyp\"\n";
  355.             push @files,"${defmimetyp}:${code}:${file}";
  356.         }
  357.     }
  358. }
  359.  
  360. unless ($action) {
  361.        if ($0 =~ m!(^|/)view$!)     { $action="view";   }
  362.     elsif ($0 =~ m!(^|/)see$!)      { $action="view";   }
  363.     elsif ($0 =~ m!(^|/)cat$!)      { $action="cat";    }
  364.     elsif ($0 =~ m!(^|/)edit$!)     { $action="edit";   }
  365.     elsif ($0 =~ m!(^|/)change$!)   { $action="edit";   }
  366.     elsif ($0 =~ m!(^|/)compose$!)  { $action="compose";}
  367.     elsif ($0 =~ m!(^|/)print$!)    { $action="print";  }
  368.     elsif ($0 =~ m!(^|/)create$!)   { $action="compose";}
  369.     else                            { $action="view";   }
  370. }
  371.  
  372.  
  373. $mailcaps = $ENV{MAILCAPS};
  374. $mailcaps = "$ENV{HOME}/.mailcap:/etc/mailcap:/usr/local/etc/mailcap:/usr/share/etc/mailcap:/usr/etc/mailcap" unless $mailcaps;
  375. foreach (split(/:/,$mailcaps)) {
  376.     ReadMailcap($_);
  377. }
  378.  
  379. foreach (@files) {
  380.     my($type,$code,$file) = m/^(.*?):(.*?):(.*)$/;
  381.     print STDERR "Processing file \"$file\" of type \"$type\" (encoding=",$code?$code:"none",")...\n" if $debug;
  382.  
  383.     if ($file ne '-') {
  384.         if ($action eq 'compose' || $action eq 'edit') {
  385.             if (-e $file) {
  386.                 if (! -w $file) {
  387.                     print STDERR "Error: no write permission for file \"$file\"\n";
  388.                     $retcode = 2 if ($retcode < 2);
  389.                     next;
  390.                 }
  391.             } else {
  392.                 if (open(TEST,">$file")) {
  393.                     close(TEST);
  394.                     unlink($file);
  395.                 } else {
  396.                     print STDERR "Error: no write permission for file \"$file\"\n";
  397.                     $retcode = 2 if ($retcode < 2);
  398.                     next;
  399.                 }
  400.             }
  401.         } else {
  402.             if (! -e $file) {
  403.                 print STDERR "Error: no such file \"$file\"\n";
  404.                 $retcode = 2 if ($retcode < 2);
  405.                 next;
  406.             }
  407.             if (! -r $file) {
  408.                 print STDERR "Error: no read permission for file \"$file\"\n";
  409.                 $retcode = 2 if ($retcode < 2);
  410.                 next;
  411.             }
  412.         }
  413.     }
  414.  
  415.     my(@matches,$entry,$res,$efile);
  416.     if ($code) {
  417.         $efile = $file;
  418.         $file  = DecodeFile($efile,$code,$action);
  419.         next unless $file;
  420.     }
  421.  
  422.     foreach $entry (@mailcap) {
  423.         $entry =~ m/^(.*?)\s*;/;
  424.         $_ = "\Q$1\E"; s/\\\*/\.\*/g;
  425.         push @matches,$entry if ($type =~ m!^$_$!i);
  426.     }
  427.     @matches = grep(/\Q$action\E=/,@matches) unless ($action eq "view" || $action eq "cat");
  428.  
  429.     my $done=0;
  430.     my $fail=0;
  431.     foreach $match (@matches) {
  432.         my $comm;
  433.         print STDERR " - checking mailcap entry \"$match\"\n" if $debug;
  434.         if ($action eq "view" || $action eq "cat") {
  435.             ($comm) = ($match =~ m/^.*?;\s*(.*?)\s*($|;)/);
  436.         } else {
  437.             ($comm) = ($match =~ m/\Q$action\E=(.*?)\s*($|;)/);
  438.         }
  439.         next if (!$comm || $comm =~ m!(^|/)false$!i);
  440.         print STDERR " - program to execute: $comm\n" if $debug;
  441.  
  442.     if ($action eq 'cat' && $match !~ m/;\s*copiousoutput\s*($|;)/) {
  443.         print STDERR " - \"copiousoutput\" is required for \"cat\" action\n" if $debug;
  444.         $fail++;
  445.         next;
  446.     }
  447.  
  448.         my($tmpfile,$tmplink);
  449.         if ($action ne 'print' && $match =~ m/;\s*needsterminal\s*($|;)/ && ! -t STDOUT) {
  450.             if ($ENV{DISPLAY}) {
  451.                 $comm = "$xtermprgrm -T '$file ($type)' -e $0 --action=$action '${type}:%s'";
  452.             } else {
  453.                 print STDERR " - no terminal available for rule (needsterminal)\n" if $debug;
  454.                 $fail++;
  455.                 next;
  456.             }
  457.         } elsif ($action eq 'view' && !$nopager && $match =~ m/;\s*copiousoutput\s*($|;)/ && $type ne 'text/plain') {
  458.             $comm .= " | $0 --action=$action text/plain:-";
  459.         }
  460.  
  461.         if ($match =~ m/;\s*test=(.*?)\s*($|;)/) {
  462.             my $test;
  463.             print STDERR " - running test: $1 " if $debug;
  464.             $test   = system "$1 >/dev/null 2>&1";
  465.             $test >>= 8;
  466.             print STDERR " (result=$test=",($test!=0?"false":"true"),")\n" if $debug;
  467.             if ($test) {
  468.                 $fail++;
  469.                 next;
  470.             }
  471.         }
  472.  
  473.         if ($file ne "-") {
  474.             if ($comm =~ m/[^%]%s/) {
  475.                 if ($file =~ m![^ a-z0-9,.:/@%^+=_-]!i) {
  476.                     $match =~ m/nametemplate=(.*?)\s*($|;)/;
  477.                     my $prefix = $1;
  478.                     my $linked = 0;
  479.                     while (!$linked) {
  480.                         $tmplink = TempFile($prefix);
  481.                         unlink($tmplink);
  482.                         if ($file =~ m!^/!) {
  483.                             $linked = symlink($file,$tmplink);
  484.                         } else {
  485.                             my $pwd = `/bin/pwd`;
  486.                             chomp($pwd);
  487.                             $linked = symlink("$pwd/$file",$tmplink);
  488.                         }
  489.                     }
  490.                     print STDERR " - filename contains shell meta-characters; aliased to '$tmplink'\n" if $debug;
  491.                     $comm =~ s/([^%])%s/$1$tmplink/g;
  492.                 } else {
  493.                     $comm =~ s/([^%])%s/$1$file/g;
  494.                 }
  495.             } else {
  496.                 if ($comm =~ m/\|/) {
  497.                     $comm =~ s/\|/<\Q$file\E \|/;
  498.                 } else {
  499.                     $comm .= " <\Q$file\E";
  500.                 }
  501.                 if ($action eq 'edit' || $action eq 'compose') {
  502.                     $comm .= " >\Q$file\E";
  503.                 }
  504.             }
  505.         } else {
  506.             if ($comm =~ m/[^%]%s/) {
  507.                 $tmpfile = SaveStdin($match);
  508.                 $comm =~ s/([^%])%s/$1$tmpfile/g;
  509.             } else {
  510.                 # no name means same as "-"... read from stdin
  511.             }
  512.         }
  513.  
  514.         $comm =~ s!([^%])%t!$1$type!g;
  515.         $comm =~ s!([^%])%F!$1!g;
  516.         $comm =~ s!%{(.*?)}!$_="'$ENV{$1}'";s/\`//g;s/\'\'//g;$_!ge;
  517.         $comm =~ s!\\(.)!$1!g;
  518.         $comm =~ s!\'\'!\'!g;
  519.         $comm =~ s!$quotedsemi!;!go;
  520.         $comm =~ s!$quotedprct!%!go;
  521.  
  522.         print STDERR " - executing: $comm\n" if $debug;
  523.     if ($norun) {
  524.         print $comm,"\n";
  525.         $res = 0;
  526.     } else {
  527.         $res = system $comm;
  528.         $res = int($res/256);
  529.     }
  530.         if ($res != 0) {
  531.             print STDERR "Warning: program returned non-zero exit code \#$res\n";
  532.             $retcode = $res;
  533.         }
  534.         $done=1;
  535.         unlink $tmpfile if $tmpfile;
  536.         unlink $tmplink if $tmplink;
  537.         last;
  538.     }
  539.  
  540.     if (!$done) {
  541.         if ($fail) {
  542.             print STDERR "Error: no \"$action\" rule for type \"$type\" passed its test case\n";
  543.             print STDERR "       (for more information, add \"--debug=1\" on the command line)\n";
  544.             $retcode = 3 if ($retcode < 3);
  545.         } else {
  546.             print STDERR "Error: no \"$action\" mailcap rules found for type \"$type\"\n";
  547.             $retcode = 3 if ($retcode < 3);
  548.         }
  549.         unlink $file if $code;
  550.         $retcode = 1 unless $retcode;
  551.         next;
  552.     }
  553.  
  554.     if ($code) {
  555.         if ($action eq 'edit' || $action eq 'compose') {
  556.             my $file = EncodeFile($file,$efile,$code);
  557.             unlink $file if $file;
  558.         } else {
  559.             unlink $file;
  560.         }
  561.     }
  562. }
  563.  
  564. exit($retcode);
  565.